<?php

namespace App\Models\V6;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\V1\ChapterModel;

class CloMemberInfo extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];
    protected $table = 'my_clo_member_user_info';
    protected $primaryKey = 'clo_id';
    protected $fillable = ['user_id', 'flower', 'introduce', 'title'];


    public function get_member_info(Request $request)
    {
        $user_info = DB::table('my_clo_member_user_info as my')
            ->select('clo_id', 'user_id', 'flower', 'introduce', 'title', 'user_img', 'm.YourName as name', 'flower_user')
            ->leftJoin('memberinfo as m', 'm.UserID', '=', 'my.user_id')
            ->get();
        $user_phone = $request->userPhone;

        foreach ($user_info as $k => &$v) {
            $flower_user = $v->flower_user;
            $data = explode(',', $flower_user);
            if (in_array($user_phone, $data)) {
                $v->flower_status = 1;
                unset($v->flower_user);
            } else {
                unset($v->flower_user);
                $v->flower_status = 0;
            }
        }
        $chapterModel = new ChapterModel();
        foreach ($user_info as $key => $value) {
            $user_info_role = $chapterModel->get_user_role($value->user_id);
            if ($user_info_role->role == 1) {
                $value->role = '普通会员';
            } elseif ($user_info_role->role == 2) {
                $value->role = '学习官';
            } elseif ($user_info_role->role == 3) {
                $value->role = '副会长';
            } elseif ($user_info_role->role == 4) {
                $value->role = '会长';
            }
        }
        return $user_info;
    }

}
